package com.lcz.authority.admin.mapper;

import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.LambdaQueryWrapperX;
import com.lcz.authority.common.domain.entity.SysUserPostEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author lcz
 * @desc 用户与岗位关联表 数据层
 * @mail lcz_0130@163.com
 * @date 2024/06/17
 */
@Mapper
public interface SysUserPostMapper extends BaseMapperX<SysUserPostEntity> {
    /**
     * 通过用户ID删除用户和岗位关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    default int deleteUserPostByUserId(Long userId) {
        //delete from sys_user_post where user_id=#{userId}
        return delete(new LambdaQueryWrapperX<SysUserPostEntity>().eq(SysUserPostEntity::getUserId, userId));
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    default int countUserPostById(Long postId) {
        //select count(1) from sys_user_post where post_id=#{postId}
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<SysUserPostEntity>().eq(SysUserPostEntity::getPostId, postId)));
    }

    /**
     * 批量删除用户和岗位关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    default int deleteUserPost(Long[] ids) {
        //delete from sys_user_post where id in (#{ids})
        return delete(new LambdaQueryWrapperX<SysUserPostEntity>().in(SysUserPostEntity::getId, ids));
    }

    /**
     * 批量新增用户岗位信息
     *
     * @param userPostList 用户岗位列表
     * @return 结果
     */
    default void batchUserPost(List<SysUserPostEntity> userPostList) {
        //insert into sys_user_post (user_id,post_id) values (#{userId},#{postId})
        insertBatchLarge(userPostList);
    }
}
